package cn.yunyichina.provider.drug.utils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;

/**
 * Created by Administrator on 2018/1/10 0010.
 */
public class ListUtils {

    public static <K, V> void listGroup2Map(List<V> list, Map<K, List<V>> map, Class<V> clazz, String methodName) {
        if (null == list || null == map || null == clazz) {
            System.out.print("ListUtils.listGroup2Map 入参错误，list：" + list + " ；map：" + map + " ；clazz：" + clazz + " ；methodName：" + methodName);
            return;
        }

        Method method = getMethodByName(clazz, methodName);
        if (null == method) {
            return;
        }

        listGroup2Map(list, map, method);
    }

    public static Method getMethodByName(Class<?> clazz, String methodName) {
        Method method = null;
        if (null == clazz) {
            System.out.print("ListUtils.getMethodByName 入参错误，clazz：" + clazz + " ；methodName：" + methodName);
            return method;
        }

        try {
            method = clazz.getDeclaredMethod(methodName);
        } catch (Exception e) {
            System.out.print("类获取方法失败！");
        }

        return method;
    }

    public static <K, V> void listGroup2Map(List<V> list, Map<K, List<V>> map, Method method) {
        if (null == list || null == map || null == method) {
            System.out.print("ListUtils.listGroup2Map 入参错误，list：" + list + " ；map：" + map + " ；method：" + method);
            return;
        }

        try {
            Object key;
            List<V> listTmp;
            for (V val : list) {
                key = method.invoke(val);
                listTmp = map.get(key);
                if (null == listTmp) {
                    listTmp = new ArrayList<V>();
                    map.put((K) key, listTmp);
                }
                listTmp.add(val);
            }
        } catch (Exception e) {
            System.out.print("分组失败！");
        }
    }

    public static <E> void Sort(List<E> list, final String method, final String className, final String sort) {
        Collections.sort(list, new Comparator() {
            public int compare(Object a, Object b) {
                int ret = 0;
                try {
                    Method m1 = ((E) a).getClass().getMethod(method, null);
                    Method m2 = ((E) b).getClass().getMethod(method, null);
                    if (sort != null && "desc".equals(sort)){
                        if("Integer".equals(className)){
                            ret = (Integer)m2.invoke(((E) b), null) - (Integer)m1.invoke(((E) a), null);
                        }if("Double".equals(className)){
                            Double d = (Double)m2.invoke(((E) b), null) - (Double)m1.invoke(((E) a), null);
                            if(d == 0){
                                ret = 0;
                            }else if(d > 0){
                                ret = 1;
                            }else{
                                ret = -1;
                            }
                        }else{
                            ret = m2.invoke(((E) b), null).toString()
                                    .compareTo(m1.invoke(((E) a), null).toString());
                        }
                    }
                    else{
                        if("Integer".equals(className)){
                            ret = (Integer)m1.invoke(((E) a), null) - (Integer)m2.invoke(((E) b), null);
                        }if("Double".equals(className)){
                            Double d = (Double)m1.invoke(((E) a), null) - (Double)m2.invoke(((E) b), null);
                            if(d == 0){
                                ret = 0;
                            }else if(d > 0){
                                ret = 1;
                            }else{
                                ret = -1;
                            }
                        }else{
                            ret = m1.invoke(((E) a), null).toString()
                                    .compareTo(m2.invoke(((E) b), null).toString());
                        }
                    }
                } catch (NoSuchMethodException ne) {
                    System.out.println(ne);
                } catch (IllegalAccessException ie) {
                    System.out.println(ie);
                } catch (InvocationTargetException it) {
                    System.out.println(it);
                }
                return ret;
            }
        });
    }
}
